//
//  LoginTableViewController
//  Login
//
//  Created by Sean on 3/23/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "LoginTableViewController.h"
#import "Login.h"
#import "FileHandler.h"

@implementation LoginTableViewController

-(id) init
{
	if(self =[super initWithStyle:UITableViewStylePlain])
	{
		alphabet = [[NSMutableArray alloc] init];
		
		for(int i=0; i < 26; i++)
		{
			char alphabetchar[2];
			alphabetchar[0] = ((char) (65 + i));
			alphabetchar[1] = '\0';
			[alphabet addObject:[NSString stringWithCString:alphabetchar encoding: NSASCIIStringEncoding]];
		}
		
		//numbers
		[alphabet addObject:@"#"];
	
		
		allLogins = [[FileHandler loadLoginsFromFile] retain];
		
		UIBarButtonItem* addButton = [[UIBarButtonItem alloc] initWithTitle:@"Add" style:UIBarButtonItemStylePlain target:self action:@selector(addLogin)];
		
		
		self.navigationItem.rightBarButtonItem = addButton;
		self.navigationItem.leftBarButtonItem = self.editButtonItem;
		
		self.tableView.rowHeight = 60;
	}
	   
	return self;
}

-(IBAction) addLogin
{
	LoginEditController* lec = [[LoginEditController alloc] initWithLogins:allLogins:nil];
	
	self.navigationController.navigationBarHidden = YES;
	[self.navigationController pushViewController:lec animated:YES];
	[lec release];
}

-(Logins*) getAllLogins
{
	return allLogins;
}


- (void)viewWillAppear:(BOOL)animated
{
	[self.tableView reloadData];
	[super viewWillAppear:YES];
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}




#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [alphabet count];
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

	NSMutableArray* alpha = [allLogins getAllValuesForAlphaIndex:section];
	
	return [alpha count];
}



//- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
//	return alphabet;
//}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
	
	if([[allLogins getAllValuesForAlphaIndex:section] count] > 0)
		return [alphabet objectAtIndex:section];
	
	
	return nil;
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    const NSInteger LOGIN_LABEL = 1001;
	const NSInteger LOGIN_NAME_LABEL = 1002;
	const NSInteger PASS_LABEL = 1003;
    
	UILabel* loginName;
	UILabel* login;
	UILabel* pass;
	
	
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
				
		loginName =
		[[[UILabel alloc]
		  initWithFrame:
		  CGRectMake(10,5,120,60)]
		 autorelease];
		[cell.contentView addSubview:loginName];
		loginName.tag = LOGIN_NAME_LABEL;
		loginName.backgroundColor = [UIColor clearColor];
		loginName.textColor = [UIColor colorWithRed:0.25 green:0.0 blue:0.0 alpha:1.0];
		loginName.highlightedTextColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.9 alpha:1.0];
		loginName.font =[UIFont boldSystemFontOfSize:20];
		
		login =
		[[[UILabel alloc]
		  initWithFrame:
		  CGRectMake(140,5,170,30)]
		 autorelease];
		[cell.contentView addSubview:login];
		login.tag = LOGIN_LABEL;
		login.backgroundColor = [UIColor clearColor];
		login.textColor = [UIColor blueColor];
		login.highlightedTextColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.9 alpha:1.0];
		login.font = [UIFont systemFontOfSize:[UIFont labelFontSize]];
		
		pass =
		[[[UILabel alloc]
		  initWithFrame:
		  CGRectMake(140,28,170,30)]
		 autorelease];
		[cell.contentView addSubview:pass];
		pass.tag = PASS_LABEL;
		pass.backgroundColor = [UIColor clearColor];
		pass.textColor = [UIColor redColor];
		pass.highlightedTextColor = [UIColor colorWithRed:1.0 green:1.0 blue:0.9 alpha:1.0];
		pass.font = [UIFont systemFontOfSize:[UIFont labelFontSize]];
    }
    else
	{
		loginName = (UILabel *)[cell viewWithTag:LOGIN_NAME_LABEL];
		login = (UILabel *)[cell viewWithTag:LOGIN_LABEL];
		pass = (UILabel *)[cell viewWithTag:PASS_LABEL];
	}
	
    NSMutableArray* alpha = [allLogins getAllValuesForAlphaIndex:indexPath.section];
	Login* l = [alpha objectAtIndex:indexPath.row];
	
	loginName.text = [l loginName];
	login.text = [l key];
	pass.text = [l value];
	
	
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	
	NSMutableArray* alpha = [allLogins getAllValuesForAlphaIndex:indexPath.section];
	Login* l = [alpha objectAtIndex:indexPath.row];
	
	LoginEditController* lec = [[LoginEditController alloc] initWithLogins:allLogins:l];
	
	self.navigationController.navigationBarHidden = YES;
	[self.navigationController pushViewController:lec animated:YES];
	[lec release];
}	





// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
		 NSMutableArray* alpha = [allLogins getAllValuesForAlphaIndex:indexPath.section];
		[alpha removeObjectAtIndex:[indexPath row]];
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    }   
   // else if (editingStyle == UITableViewCellEditingStyleInsert) {
//        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
//    }   
}




- (void)dealloc {
    [super dealloc];
}


@end

